{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "63372822",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1\n",
      "Data: tensor([[ 7,  8],\n",
      "        [ 9, 10]]), Labels: tensor([3, 4])\n",
      "Data: tensor([[5, 6],\n",
      "        [3, 4]]), Labels: tensor([2, 1])\n",
      "Data: tensor([[11, 12],\n",
      "        [ 1,  2]]), Labels: tensor([5, 0])\n",
      "Epoch 2\n",
      "Data: tensor([[ 9, 10],\n",
      "        [ 5,  6]]), Labels: tensor([4, 2])\n",
      "Data: tensor([[7, 8],\n",
      "        [1, 2]]), Labels: tensor([3, 0])\n",
      "Data: tensor([[ 3,  4],\n",
      "        [11, 12]]), Labels: tensor([1, 5])\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "from torch.utils.data import DataLoader, TensorDataset\n",
    "\n",
    "\"\"\"1.TensorDataset()的用法\"\"\"\n",
    "# 假设有一些数据和标签\n",
    "data = torch.tensor([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])\n",
    "labels = torch.tensor([0, 1, 2, 3, 4, 5])\n",
    "\n",
    "# 创建一个 TensorDataset 实例\n",
    "dataset = TensorDataset(data, labels)\n",
    "\n",
    "# 创建 DataLoader\n",
    "dataloader = DataLoader(dataset, batch_size=2, shuffle=True)\n",
    "\n",
    "# 使用 DataLoader\n",
    "for epoch in range(2):  # 假设我们迭代两个 epochs\n",
    "    print(f\"Epoch {epoch+1}\")\n",
    "    for batch_data, batch_labels in dataloader:  # 即使\"shuffle=True\"打乱顺序，data与labels依然一一对应\n",
    "        print(f\"Data: {batch_data}, Labels: {batch_labels}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1f3480bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<zip object at 0x000001DDF04E54C0>\n",
      "[(1, 'a'), (2, 'b'), (3, 'c')]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"2.zip(a,b)的用法\"\"\"\n",
    "# 定义两个列表\n",
    "list1 = [1, 2, 3]\n",
    "list2 = ['a', 'b', 'c']\n",
    "\n",
    "# 使用 zip() 函数将 list1 和 list2 组合在一起\n",
    "zipped = zip(list1, list2)\n",
    "\n",
    "# 打印 zipped，它会显示一个 zip 对象\n",
    "print(zipped)\n",
    "\n",
    "# 为了查看 zip 对象中的内容，我们可以将其转换为列表\n",
    "zipped_list = list(zipped)\n",
    "\n",
    "# 打印转换后的列表，它会显示成对元组的列表\n",
    "print(zipped_list)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ac669512",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[7, 13, 19]\n"
     ]
    }
   ],
   "source": [
    "\"\"\"3.列表推导式\"\"\"\n",
    "data = [a+b for a, b in [(3,4),(6,7),(9,10)]]\n",
    "print(data)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "d2l-1",
   "language": "python",
   "name": "d2l-1"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
